class TGrenadeProj extends TFGrenade; //Projectile;

var() vector ShakeRotMag;           // how far to rot view
var() vector ShakeRotRate;          // how fast to rot view
var() float  ShakeRotTime;          // how much time to rot the instigator's view
var() vector ShakeOffsetMag;        // max view offset vertically
var() vector ShakeOffsetRate;       // how fast to offset view vertically
var() float  ShakeOffsetTime;       // how much time to offset view
var bool bExploded;
/*var float ExplodeTimer;
var() float DampenFactor, DampenFactorParallel;
var bool bTimerSet;
var bool bHitWater;

replication
{
    reliable if (Role==ROLE_Authority)
        ExplodeTimer;
}
*/
simulated function PostBeginPlay()
{
   if ( bDeleteMe || IsInState('Dying') )
	return;
    if ( Role == ROLE_Authority )
    {
        Velocity = Speed * Vector(Rotation);
        RandSpin(25000);
        if (Instigator.HeadVolume.bWaterVolume)
        {
            bHitWater = true;
            Velocity = 0.6*Velocity;
        }
    }
    if ( Instigator != None )
		InstigatorController = Instigator.Controller;
}

//simulated function PostNetBeginPlay()
//{
//	if ( Physics == PHYS_None )
//    {
//        SetTimer(ExplodeTimer, false);
//        bTimerSet = true;
//    }
//}

simulated function ProcessTouch( actor Other, vector HitLocation )
{
    if ( Pawn(Other) != None && (Other != Instigator || bCanHitOwner) )
    {
		Explode(HitLocation, Normal(HitLocation-Other.Location));
    }
}

//simulated function Timer()
//{
//    Explode(Location, vect(0,0,1));
//}

simulated function Explode(vector HitLocation, vector HitNormal)
{
	BlowUp(HitLocation);
}

//simulated function Landed( vector HitNormal )
//{
//    HitWall( HitNormal, None );
//}

//simulated function HitWall( vector HitNormal, actor Wall )
//{
//    local Vector VNorm;

//    // Reflect off Wall w/damping
//    VNorm = (Velocity dot HitNormal) * HitNormal;
//    Velocity = -VNorm * DampenFactor + (Velocity - VNorm) * DampenFactorParallel;

//    RandSpin(100000);
//    Speed = VSize(Velocity);

//    if ( Speed < 20 )
//    {
//        bBounce = False;
//        SetPhysics(PHYS_None);
//        SetTimer(ExplodeTimer, false);
//        PlaySound(Sound'HolyHanTGrenade.RunAway');
//    }
//    else if ( (Level.NetMode != NM_DedicatedServer) && (Speed > 250) )
//	PlaySound(ImpactSound, SLOT_Misc,,,,,true);
//}

function BlowUp(vector HitLocation)
{
    //Spawn(class'TGrenadeExplosion',,, HitLocation - 100 * Normal(Velocity), Rot(0,16384,0));
	MakeNoise(1.0);
	SetPhysics(PHYS_None);
	bHidden = true;
    GotoState('Dying');
}

state Dying
{
	function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation,
							Vector momentum, class<DamageType> damageType) {}

    function BeginState()
    {
		bHidden = true;
		SetPhysics(PHYS_None);
		SetCollision(false,false,false);
		if ( !bExploded )
		{
			Spawn(class'TGrenadeExplosion',,, Location, Rotation);
			Spawn(class'TGrenadeNova',,, Location, Rotation);
//			Spawn(class'TGrenadeBurst',,, Location, Rotation);
//			Spawn(class'TGrenadeSmokeRing',,, Location, Rotation);
			ShakeView();
		}
		InitialState = 'Dying';
    }

    function ShakeView()
    {
        local Controller C;
        local PlayerController PC;
        local float Dist, Scale;

        for ( C=Level.ControllerList; C!=None; C=C.NextController )
        {
            PC = PlayerController(C);
            if ( PC != None && PC.ViewTarget != None )
            {
                Dist = VSize(Location - PC.ViewTarget.Location);
                if ( Dist < DamageRadius * 2.0)
                {
                    if (Dist < DamageRadius)
                        Scale = 1.0;
                    else
                        Scale = (DamageRadius*2.0 - Dist) / (DamageRadius);
                    C.ShakeView(ShakeRotMag*Scale, ShakeRotRate, ShakeRotTime, ShakeOffsetMag*Scale, ShakeOffsetRate, ShakeOffsetTime);
                }
            }
        }
    }

Begin:
    PlaySound(sound'WeaponSounds.TAGRifle.IonCannonBlast'); //redeemer_explosionsound');
    HurtRadius(Damage, DamageRadius*0.1, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.2, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.3, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.4, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.5, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.6, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.7, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.8, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.9, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*1.0, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.9, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.8, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.7, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.6, MyDamageType, MomentumTransfer, Location);
    Sleep(0.1);
    HurtRadius(Damage, DamageRadius*0.5, MyDamageType, MomentumTransfer, Location);
    Destroy();
}

defaultproperties
{
     ShakeRotMag=(Z=200.000000)
     ShakeRotRate=(Z=2000.000000)
     ShakeRotTime=4.000000
     ShakeOffsetMag=(Z=8.000000)
     ShakeOffsetRate=(Z=150.000000)
     ShakeOffsetTime=7.000000
     ExplodeTimer=0.400000
     Speed=200.000000
     MaxSpeed=600.000000
     TossZ=220.000000
     Damage=40.000000
     DamageRadius=1500.000000
     MomentumTransfer=10000.000000
     MyDamageType=Class'Taskforces.TGrenadeDamType'
     LightHue=28
     LightBrightness=255.000000
     LightRadius=6.000000
     StaticMesh=StaticMesh'TFMedia.DGrenade'
     bDynamicLight=True
     bNetTemporary=False
     DrawScale=0.800000
     Skins(0)=Texture'TFMedia.TGrenade.ThermoGrenadeTex1'
     AmbientGlow=96
     bUnlit=False
     SoundVolume=255
     SoundRadius=100.000000
     TransientSoundVolume=1.000000
     TransientSoundRadius=5000.000000
     CollisionRadius=24.000000
     CollisionHeight=12.000000
     RotationRate=(Yaw=8000,Roll=8000)
     DesiredRotation=(Pitch=0,Yaw=80000,Roll=80000)
     ForceRadius=100.000000
     ForceScale=5.000000
}
